package com.zenithmind.classroom.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.classroom.pojo.domain.CourseChapter;
import com.zenithmind.classroom.pojo.vo.ChapterVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 课程章节Mapper接口
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface CourseChapterMapper extends BaseMapper<CourseChapter> {

    /**
     * 获取课程章节列表（包含用户进度）
     * 
     * @param courseId 课程ID
     * @param userId 用户ID
     * @param includeUnpublished 是否包含未发布
     * @return 章节列表
     */
    List<ChapterVO> selectCourseChaptersWithProgress(@Param("courseId") String courseId, 
                                                    @Param("userId") String userId, 
                                                    @Param("includeUnpublished") Boolean includeUnpublished);

    /**
     * 获取章节详情（包含用户进度）
     * 
     * @param chapterId 章节ID
     * @param userId 用户ID
     * @return 章节详情
     */
    ChapterVO selectChapterDetailWithProgress(@Param("chapterId") String chapterId, @Param("userId") String userId);

    /**
     * 更新章节统计
     * 
     * @param chapterId 章节ID
     * @return 影响行数
     */
    @Update("UPDATE classroom_course_chapter SET " +
            "lesson_count = (SELECT COUNT(*) FROM classroom_course_lesson WHERE chapter_id = #{chapterId} AND deleted = 0), " +
            "estimated_minutes = (SELECT COALESCE(SUM(duration_minutes), 0) FROM classroom_course_lesson WHERE chapter_id = #{chapterId} AND deleted = 0), " +
            "update_time = NOW() " +
            "WHERE id = #{chapterId}")
    int updateChapterStatistics(@Param("chapterId") String chapterId);

    /**
     * 获取章节学习进度
     * 
     * @param chapterId 章节ID
     * @param userId 用户ID
     * @return 进度百分比
     */
    @Select("SELECT COALESCE(AVG(lp.progress_percentage), 0) " +
            "FROM learning_progress lp " +
            "INNER JOIN classroom_course_lesson cl ON lp.lesson_id = cl.id " +
            "WHERE cl.chapter_id = #{chapterId} AND lp.user_id = #{userId} AND lp.deleted = 0")
    Integer getChapterProgress(@Param("chapterId") String chapterId, @Param("userId") String userId);

    /**
     * 批量更新章节排序
     * 
     * @param chapterIds 章节ID列表（按新顺序）
     * @param courseId 课程ID
     * @return 影响行数
     */
    int batchUpdateChapterOrder(@Param("chapterIds") List<String> chapterIds, @Param("courseId") String courseId);

    /**
     * 获取章节最大排序值
     * 
     * @param courseId 课程ID
     * @return 最大排序值
     */
    @Select("SELECT COALESCE(MAX(sort_order), 0) FROM classroom_course_chapter WHERE course_id = #{courseId} AND deleted = 0")
    Integer getMaxSortOrder(@Param("courseId") String courseId);

    /**
     * 检查章节是否属于指定课程
     * 
     * @param chapterId 章节ID
     * @param courseId 课程ID
     * @return 是否属于
     */
    @Select("SELECT COUNT(*) > 0 FROM classroom_course_chapter WHERE id = #{chapterId} AND course_id = #{courseId} AND deleted = 0")
    Boolean checkChapterBelongsToCourse(@Param("chapterId") String chapterId, @Param("courseId") String courseId);

    /**
     * 获取下一个章节
     * 
     * @param courseId 课程ID
     * @param currentSortOrder 当前排序值
     * @return 下一个章节
     */
    @Select("SELECT * FROM classroom_course_chapter " +
            "WHERE course_id = #{courseId} AND sort_order > #{currentSortOrder} AND deleted = 0 " +
            "ORDER BY sort_order ASC LIMIT 1")
    CourseChapter selectNextChapter(@Param("courseId") String courseId, @Param("currentSortOrder") Integer currentSortOrder);

    /**
     * 获取上一个章节
     * 
     * @param courseId 课程ID
     * @param currentSortOrder 当前排序值
     * @return 上一个章节
     */
    @Select("SELECT * FROM classroom_course_chapter " +
            "WHERE course_id = #{courseId} AND sort_order < #{currentSortOrder} AND deleted = 0 " +
            "ORDER BY sort_order DESC LIMIT 1")
    CourseChapter selectPreviousChapter(@Param("courseId") String courseId, @Param("currentSortOrder") Integer currentSortOrder);

    /**
     * 复制章节
     * 
     * @param sourceChapterId 源章节ID
     * @param targetCourseId 目标课程ID
     * @param newTitle 新标题
     * @param newSortOrder 新排序值
     * @return 新章节ID
     */
    String copyChapter(@Param("sourceChapterId") String sourceChapterId, 
                      @Param("targetCourseId") String targetCourseId, 
                      @Param("newTitle") String newTitle, 
                      @Param("newSortOrder") Integer newSortOrder);
}
